<?php
/**
 * PHPExcel
 *
 * Copyright (C) 2006 - 2011 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel
 * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
 * @version    1.7.6, 2011-02-27
 */

/** Error reporting */
error_reporting(0);

date_default_timezone_set('Europe/London');

/** PHPExcel_IOFactory */
require_once 'Classes/PHPExcel/IOFactory.php';
require_once 'conn.ini';

//echo date('H:i:s') . " Load from Excel5 template\n";
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load("templates/reminder_template.xls");

$data = null;
$from = (isset($_GET['from']) && $_GET['from'] != 0) ? $_GET['from'] : date('Y-m-d', strtotime(date('Y-m-d H:i:s')) + 7 * 24 * 60 * 60);
$to = (isset($_GET['to']) && $_GET['to'] > 0) ? $_GET['to'] : date('Y-m-d');
if($from > 0 && $to > 0 && strtotime($to) > strtotime($from)) {
    $sql = "SELECT * FROM cc_reminders WHERE dtstart >= '" . $from . "' AND dtstart <= '" . $to . "'";
    $rs = mysql_query($sql);
    if(mysql_num_rows($rs)) {
        $i=0;
        while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
            $sql = "SELECT * FROM cc_paymentschedules WHERE id = '" . $row['paymentschedule_id'] . "'";
            $sub_rs = mysql_query($sql);
            $sub_row = mysql_fetch_array($sub_rs, MYSQL_ASSOC);
            $unpaid = (int) $sub_row['broker_amount'] - (int) $sub_row['paid_amount'];
            if($unpaid > 0) {
                $data[$i]['date'] = $row['dtstart'];
                $data[$i]['event_name'] = $row['summary'];
                $data[$i]['start_date'] = $row['dtstart'];
                $data[$i]['start_time'] = '00:00';
                $data[$i]['end_date'] = $row['dtstart'];
                $data[$i]['end_time'] = '23:59';
                $data[$i]['description'] = 'Unpaid Amount: ' . number_format($unpaid, 2) . ' ' . $sub_row['currency'];
                $i++;
            }
        }
    }
}

//echo date('H:i:s') . " Add new data to the template\n";
//$data = array(array(
//                    'date' => '09/16/2012',
//                    'event_name' => '135 / 1118-MAHM-112011 - Street Exchange Insurance Brokers pay to AIH',
//					'start_date' => '09/16/2012',
//					'start_time' => '00:00',
//					'end_date' => '09/16/2012',
//					'end_time' => '23:59',
//					'description'	=> 'Unpaid Amount: 278,250.00 PHP'
//				   )
//			 );

//$objPHPExcel->getActiveSheet()->setCellValue('D1', PHPExcel_Shared_Date::PHPToExcel(time()));

$baseRow = 12;
if($data != null) {
    $index = 0;
    foreach($data as $r => $dataRow) {
        $row = $baseRow + $r;
        $objPHPExcel->getActiveSheet()->insertNewRowBefore($row,1);

        $objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $r+1);
        $objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $dataRow['date']);
        $objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $dataRow['event_name']);
        $objPHPExcel->getActiveSheet()->setCellValue('E'.$row, $dataRow['start_date']);
        $objPHPExcel->getActiveSheet()->setCellValue('F'.$row, $dataRow['start_time']);
        $objPHPExcel->getActiveSheet()->setCellValue('G'.$row, $dataRow['end_date']);
        $objPHPExcel->getActiveSheet()->setCellValue('H'.$row, $dataRow['end_time']);
        $objPHPExcel->getActiveSheet()->setCellValue('I'.$row, $dataRow['description']);
        $objPHPExcel->getActiveSheet()->getRowDimension($row)->setRowHeight(-1);
        $index++;
    }
    $objPHPExcel->getActiveSheet()->removeRow($baseRow-1,1);
    $objPHPExcel->getActiveSheet()->removeRow($baseRow+$index-1,1);
}

mysql_close();
//echo date('H:i:s') . " Write to Excel5 format\n";
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="reminder.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//$objWriter->save(str_replace('.php', '.xls', __FILE__));
//save to file
$objWriter->save('php://output');
exit;

// Echo memory peak usage
//echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r\n";

// Echo done
//echo date('H:i:s') . " Done writing file.\r\n";
